<?php

namespace App\Http\Controllers\Api;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Facades\Response;
use Intervention\Image\Facades\Image;
use Illuminate\Support\Facades\Storage;


class UploadController extends Controller
{



    public function store(Request $request)
    {

        $request->validate([
            'image' => 'required|image|max:2048'
        ]);

        try {

            $image = $request->file('image');
            $filename = date("Y-m-d").'_'.$image->getClientOriginalName();
            Storage::disk('public')->put("images/user/".$filename, file_get_contents($image));

            return $this->success("/api/v1/image/user/".$filename.'/');

        }catch (\Exception $exception){

            return false;
        }


    }

    //显示图片
    public function show($pa,$filename)
    {
        $path = public_path('storage/images/'.$pa.'/'. $filename); // 注意这里使用 public_path 而不是 storage_path


        if (!File::exists($path)) {
            abort(404);
        }

        $headers = [
            'Content-Type' => 'image/jpeg', // 根据你的图片类型修改 MIME 类型
        ];

        return response()->file($path, $headers);

    }



}
